<?php

namespace App\Api\Controllers;

use App\Api\Controllers\BaseController;
use App\Models\Option;
use Illuminate\Http\Request;

use App\Api\Service\SearchService;

class SearchController extends BaseController
{
    protected $service;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->service = new SearchService();
    }

    public function search_test(\App\Api\Requests\Search\SearchRequest $request)
    {
        $search = $request->input();
        $op = Option::where('map_ids', 'like', "%" . $search['college_tab'] . "%")->pluck('option');
        return success(1, $op);
    }

    /**
     * 搜索
     *
     * @param Request $request
     * @return void
     */
    public function search(\App\Api\Requests\Search\SearchRequest $request)
    {
        $search['sex'] = $request->input('sex');
        $search['min_age'] = $request->input('min_age', 1) ?? 1;
        $search['max_age'] = $request->input('max_age', 150) ?? 150;
        $search['height'] = $request->input('height', '100-250') ?? '100-250';
        $search['city'] = $request->input('city', '') ?? '';
        $search['college_tab'] = $request->input('college_tab', '') ?? '';
        $search['face_score'] = $request->input('face_score', '') ?? '';
        $search['family'] = $request->input('family', '') ?? '';
        $search['income'] = $request->input('income', '') ?? '';
        $search['constellation'] = $request->input('constellation', '') ?? '';
        $search['carandhouse'] = $request->input('carandhouse', '') ?? '';
        $search['keyword'] = $request->input('keyword', '') ?? '';
        return success('搜索结果', $this->service->get_search_result($this->uid, $search));
    }

    /**
     * 获取搜索详情
     *
     * @param Request $request
     * @return void
     */
    public function search_detail(Request $request)
    {
        $number = $request->input('number', 1) ?? 1;
        $number -= 1;
        return success('搜索信息详情', $this->service->get_search_detail($this->uid, $number));
    }

    public function log_detail(Request $request)
    {
        $id = $request->input('id', 0);
        $uid = $request->input('uid', 0);
        if ($id == 0) {
            error("记录不存在");
        }
//        if (substr($id, 0, 1) == 'i') {
//            $id = trim(substr_replace("i", " ", $id));
//        }

        return success('信息详情', $this->service->get_search_detail($uid, $id, 1));
    }

    /**
     * 收藏/取消收藏
     *
     * @param Request $request
     * @return void
     */
    public function collect(Request $request)
    {
        $search_id = $request->input('search_id', '') ?? '';
        [$result, $message] = $this->service->collect_search_data($this->uid, $search_id);
        return success($message, ['is_collect' => boolval($result)]);
    }

    /**
     * 支付查看联系方式
     *
     * @param Request $request
     * @return void
     */
    public function pay_contact(Request $request)
    {
        $search_id = $request->input('search_id', '') ?? '';
        $type = $request->input('type', '') ?? '';
        return success('购买成功', [$type => $this->service->get_contact($this->uid, $search_id, $type)]);
    }
}
